package org.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.chen.entity.Model;
import org.chen.entity.Voice;
import org.chen.mapper.VoiceMapper;
import org.chen.query.VoiceQueryParams;
import org.chen.request.VoiceRequest;
import org.chen.service.VoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
public class VoiceServiceImpl extends ServiceImpl<VoiceMapper, Voice> implements VoiceService {
    @Autowired
    private VoiceMapper voiceMapper;

    @Override
    public Page<Voice> queryVoices(VoiceQueryParams params) {
        Page<Voice> page = new Page<>(params.getPage(), params.getPageSize());

        LambdaQueryWrapper<Voice> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(params.getLanguage())){
            queryWrapper.eq(Voice::getLang,params.getLanguage());
        }

        // 按创建时间倒序排序
        queryWrapper.orderByDesc(Voice::getCreateTime);

        return voiceMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Voice saveVoice(VoiceRequest voiceRequest) {
        Voice voice = new Voice();
        voice.setLang(voiceRequest.getLang());
        voice.setAsrFormatAudioUrl(voiceRequest.getAsr_format_audio_url());
        voice.setReferenceAudioText(voiceRequest.getReference_audio_text());
        voice.setOriginAudioPath(voiceRequest.getOrigin_audio_path());
        voice.setCreateTime(LocalDateTime.now());
        voice.setUpdateTime(LocalDateTime.now());
        voiceMapper.insert(voice);

        return voice;
    }
}
